import { hasToken } from '@/utils/storage'
import { Redirect, Route, RouteProps } from 'react-router-dom'

interface PrivateRouteType extends RouteProps {
  component: any
}
export default function PrivateRoute({
  children,
  component: Component,
  ...rest
}: PrivateRouteType) {
  return (
    <Route
      {...rest}
      render={
        ({ location }) => {
          if (hasToken()) {
            return children ? children : <Component></Component>
          } else {
            return (
              <Redirect
                to={{
                  pathname: '/login',
                  state: { from: location.pathname },
                }}
              />
            )
          }
        }
        //判断是否登录
      }
    />
  )
}
